package com.hwadee.property.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hwadee.property.pojo.ChargeItem;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface ChargeItemMapper extends BaseMapper<ChargeItem> {

    // 统计总记录数
    @Select("<script>" +
            "SELECT COUNT(*) FROM charge_item " +
            "<where> " +
            "   <if test='chargeName != null and chargeName.trim() != \"\"'> " +
            "       AND charge_name LIKE CONCAT('%', #{chargeName}, '%') " +
            "   </if> " +
            "   <if test='communityId != null'> " +
            "       AND community_id = #{communityId} " +
            "   </if> " +
            "   <if test='amount != null'> " +
            "       AND amount = #{amount} " +
            "   </if> " +
            "</where>" +
            "</script>")
    int countChargeItems(@Param("chargeName") String chargeName,
                         @Param("communityId") Long communityId,
                         @Param("amount") Double amount);

    // 分页查询
    @Select("<script>" +
            "SELECT * FROM charge_item " +
            "<where> " +
            "   <if test='chargeName != null and chargeName.trim() != \"\"'> " +
            "       AND charge_name LIKE CONCAT('%', #{chargeName}, '%') " +
            "   </if> " +
            "   <if test='communityId != null'> " +
            "       AND community_id = #{communityId} " +
            "   </if> " +
            "   <if test='amount != null'> " +
            "       AND amount = #{amount} " +
            "   </if> " +
            "</where> " +
            "ORDER BY create_time ASC " +
            "LIMIT #{size} OFFSET #{offset}" +
            "</script>")
    List<ChargeItem> getChargeItems(@Param("chargeName") String chargeName,
                                    @Param("communityId") Long communityId,
                                    @Param("amount") Double amount,
                                    @Param("size") int size,
                                    @Param("offset") int offset);
}
